/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */

var removeElements = function (head, val) {
    if (!head) {
        return null
    }
    let dummy = new ListNode()
    dummy.next = head
    let pre = dummy
    let cur = head
    while (cur) {
        // 如果找到了 则删除结点
        if (cur.val === val) {
            pre.next = cur.next
            cur = pre.next
        } 
        // 否则将前驱结点和当前结点向后移一位
        else {
            pre = cur
            cur = cur.next
        }
    }
    return dummy.next
};